﻿@page "/Bluetooth"
@attribute [TabItemOption(Text = "Bluetooth & Printer 蓝牙和打印")]

<h4>打印标签/条码/QR码</h4>
<h6>(连接设备后再执行打印等操作,根据打印机特性写入相应ESC/POS/CPCL命令)</h6>

@if (ShowUI)
{
    <span>内置UI</span>

    <Printer OnResult="OnResult" ShowUI="true" OnError="OnError" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" OnGetDevices="OnGetDevices" />

}
else
{
    <span>
        基本用法
    </span>
    <Printer @ref="printer" OnResult="OnResult" OnError="OnError" OnUpdateStatus="OnUpdateStatus" OnUpdateError="OnError" OnGetDevices="OnGetDevices" />
    <div @ref="printer.PrinterElement">
        <button data-action="btnConnect" class="btn btn-outline-primary">连接</button>
        <button data-action="btnDisconnect" class="btn btn-outline-danger">断开</button>
        @*<button data-action="btnReconnect" class="btn btn-outline-secondary">重连</button>*@
        <button data-action="tools" class="btn btn-outline-primary" @onclick="printer.Print">@printer.PrintButtonText</button>
    </div>
}
<pre>@message</pre>
<pre style="color:green">@statusmessage</pre>
<pre style="color:red">@errmessage</pre>
<p/>
<button class="btn btn-link" @onclick="SwitchUI ">切换界面</button>
<hr />


@code{

    Printer printer { get; set; } = new Printer();

    /// <summary>
    /// 显示内置界面
    /// </summary>
    bool ShowUI { get; set; } = false;

    private string? message;
    private string? statusmessage;
    private string? errmessage;

    private Task OnResult(string message)
    {
        this.message = message;
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnUpdateStatus(string message)
    {
        this.statusmessage = message;
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnError(string message)
    {
        this.errmessage = message;
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnGetDevices(List<string>? devices)
    {
        this.message = "";
        if (devices == null || devices.Count == 0) return Task.CompletedTask;
        this.message += $"已配对设备{devices.Count}:{Environment.NewLine}";
        devices.ForEach(a => this.message += $"   {a}{Environment.NewLine}");
        //this.message = this.message.Replace(Environment.NewLine, "<br/>");
        StateHasChanged();
        return Task.CompletedTask;
    }

    public void SwitchUI()
    {
        ShowUI = !ShowUI;
    }


}

<BtHeartratePage />
<hr />
<BtBatteryLevelPage />
<hr />

<AttributeTable Title="Printer 组件" Items="@GetAttributes()" />

<AttributeTable Title="BatteryLevel 组件" Items="@GetAttributesBatteryLevel()" />

<AttributeTable Title="Heartrate 组件" Items="@GetAttributesHeartrate()" />

<AttributeTable Title="BluetoothDevice 类" Items="@GetBluetoothDeviceAttributes()" />

<AttributeTable Title="PrinterOption 类" Items="@GetPrinterOptionAttributes()" />

@code{

    /// <summary>
    /// 获得属性方法
    /// </summary>
    /// <returns></returns>
    protected IEnumerable<AttributeItem> GetAttributes() => new AttributeItem[]
    {
        new AttributeItem() {
            Name = "Commands",
            Description = "打印指令(cpcl/esp/pos代码)",
            Type = "string?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "Print",
            Description = "打印",
            Type = "async Task",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateStatus",
            Description = "状态更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateError",
            Description = "错误更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "PrinterElement",
            Description = "UI界面元素的引用对象,为空则使用整个页面",
            Type = "ElementReference",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "Opt",
            Description = "打印机选项",
            Type = "PrinterOption",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "ShowUI",
            Description = "获得/设置 显示内置UI",
            Type = "bool",
            ValueList = "True|False",
            DefaultValue = "False"
        },
        new AttributeItem() {
            Name = "Debug",
            Description = "获得/设置 显示log",
            Type = "bool",
            ValueList = "True|False",
            DefaultValue = "False"
        },
        new AttributeItem() {
            Name = "Devicename",
            Description = "获得/设置 设备名称",
            Type = "string?",
            ValueList = "-",
            DefaultValue = "-"
        },
                        };

    /// <summary>
    /// 获得属性方法
    /// </summary>
    /// <returns></returns>
    protected IEnumerable<AttributeItem> GetPrinterOptionAttributes() => new AttributeItem[]
    {
        new AttributeItem() {
            Name = "NamePrefix",
            Description = "初始搜索设备名称前缀,默认null",
            Type = "string?",
            ValueList = "-",
            DefaultValue = "null"
        },
        new AttributeItem() {
            Name = "MaxChunk",
            Description = "数据切片大小,默认100",
            Type = "int",
            ValueList = "-",
            DefaultValue = "100"
        },
                        };

    /// <summary>
    /// 获得蓝牙设备类
    /// </summary>
    /// <returns></returns>
    protected IEnumerable<AttributeItem> GetBluetoothDeviceAttributes() => new AttributeItem[]
    {
        new AttributeItem() {
            Name = "Name",
            Description = "设备名称",
            Type = "string?",
            ValueList = "-",
            DefaultValue = "null"
        },
        new AttributeItem() {
            Name = "Value",
            Description = "设备数值:例如心率/电量%",
            Type = "decimal?",
            ValueList = "-",
            DefaultValue = "null"
        },
        new AttributeItem() {
            Name = "Status",
            Description = "状态",
            Type = "string?",
            ValueList = "-",
            DefaultValue = "null"
        },
        new AttributeItem() {
            Name = "Error",
            Description = "错误",
            Type = "string?",
            ValueList = "-",
            DefaultValue = "null"
        },

                        };

    /// <summary>
    /// 获得属性方法
    /// </summary>
    /// <returns></returns>
    protected IEnumerable<AttributeItem> GetAttributesBatteryLevel() => new AttributeItem[]
    {
        new AttributeItem() {
            Name = "GetBatteryLevel",
            Description = "查询电量",
            Type = "async Task",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateValue",
            Description = "数值更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateStatus",
            Description = "状态更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateError",
            Description = "错误更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "BatteryLevelElement",
            Description = "UI界面元素的引用对象",
            Type = "ElementReference",
            ValueList = "-",
            DefaultValue = "-"
        },
                        };


    /// <summary>
    /// 获得属性方法
    /// </summary>
    /// <returns></returns>
    protected IEnumerable<AttributeItem> GetAttributesHeartrate() => new AttributeItem[]
    {
        new AttributeItem() {
            Name = "GetHeartrate",
            Description = "连接心率带",
            Type = "async Task",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "StopHeartrate",
            Description = "停止监听心率",
            Type = "async Task",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateValue",
            Description = "数值更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateStatus",
            Description = "状态更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "OnUpdateError",
            Description = "错误更新回调方法",
            Type = "Func<string, Task>?",
            ValueList = "-",
            DefaultValue = "-"
        },
        new AttributeItem() {
            Name = "HeartrateElement",
            Description = "UI界面元素的引用对象",
            Type = "ElementReference",
            ValueList = "-",
            DefaultValue = "-"
        },
                        };
}